from Lib.SerialPort import *
from Lib.Utils import *
import datetime
import time
import os
import threading
class RelayControl(object):
    """description of class"""
    def __init__(self):
        self.seria = SerialPort('/dev/serial_3',9600)
        self.RELAY_ALL_ON='01 0F 00 00 00 08 02 FF FF E5 30'
        self.RELAY_ALL_OFF='01 0F 00 00 00 08 02 00 00 E4 80'
        self.RELAY_1_ON='01 05 00 00 ff 00 8c 3a'
        self.RELAY_1_OFF='01 05 00 00 00 00 cd ca'
        self.RELAY_2_ON='01 05 00 01 ff 00 dd fa'
        self.RELAY_2_OFF='01 05 00 01 00 00 9c 0a'
        self.RELAY_3_ON='01 05 00 02 ff 00 2d fa'
        self.RELAY_3_OFF='01 05 00 02 00 00 6c 0a'
        self.RELAY_4_ON='01 05 00 03 FF 00 7C 3A'
        self.RELAY_4_OFF='01 05 00 03 00 00 3D CA'
        self.RELAY_5_ON='01 05 00 04 FF 00 CD FB'
        self.RELAY_5_OFF='01 05 00 04 00 00 8C 0B'
        self.RELAY_6_ON='01 05 00 05 FF 00 9C 3B'
        self.RELAY_6_OFF='01 05 00 05 00 00 DD CB'
        self.RELAY_7_ON='01 05 00 06 FF 00 6C 3B'
        self.RELAY_7_OFF='01 05 00 06 00 00 2D CB'
        self.RELAY_8_ON='01 05 00 07 FF 00 3D FB'
        self.RELAY_8_OFF='01 05 00 07 00 00 7c 0b'

        self.abnormalFlag=False
        self.abnormalThread=None
        self.comFlag=False
        self.comThread=None
        print('8路继电初始化成功')
        
    # 开机运行灯
    def run(self,swich):
        if(swich):
            self.seria.Write(bytes.fromhex(self.RELAY_8_ON))
        else:
            self.seria.Write(bytes.fromhex(self.RELAY_8_OFF))
    # 风机
    def fengji(self,swich):
        if(swich):
            self.seria.Write(bytes.fromhex(self.RELAY_1_ON))
        else:
            self.seria.Write(bytes.fromhex(self.RELAY_1_OFF))
    # 压缩机
    def yansuoji(self,swich):
        if(swich):
            self.seria.Write(bytes.fromhex(self.RELAY_2_ON))
        else:
            self.seria.Write(bytes.fromhex(self.RELAY_2_OFF))

    #def abnormal(self,swich):
    #    if(swich):
    #        if(self.abnormalThread==None):
    #            self.abnormalThread = threading.Thread(target=self.abnormalSharp)
    #    else:
    #        self.abnormalFlag=False
    # 蜂鸣器
    def fengmingqi(self,swich):
        if(swich):
            self.seria.Write(bytes.fromhex(self.RELAY_3_ON))
        else:
            self.seria.Write(bytes.fromhex(self.RELAY_3_OFF))

    #def motor(self,swich):
    #    if(swich):
    #        self.seria.Write(bytes.fromhex(self.RELAY_8_ON))
    #    else:
    #        self.seria.Write(bytes.fromhex(self.RELAY_8_OFF))
    # 1路灯
    def light1(self,swich):
        if(swich):
            self.seria.Write(bytes.fromhex(self.RELAY_5_ON))
            time.sleep(0.01)
            self.seria.Write(bytes.fromhex(self.RELAY_4_ON))
        else:
            self.seria.Write(bytes.fromhex(self.RELAY_4_OFF))
            time.sleep(0.01)
            self.seria.Write(bytes.fromhex(self.RELAY_5_OFF))

    # 2路灯
    def light2(self,swich):
        if(swich):
            self.seria.Write(bytes.fromhex(self.RELAY_6_ON))
            time.sleep(0.01)
            self.seria.Write(bytes.fromhex(self.RELAY_4_ON))
        else:
            self.seria.Write(bytes.fromhex(self.RELAY_4_OFF))
            time.sleep(0.01)
            self.seria.Write(bytes.fromhex(self.RELAY_6_OFF))

    # python板启动
    def pythonBoard(self,swich):
        if(swich):
            self.seria.Write(bytes.fromhex(self.RELAY_7_ON))
        else:
            self.seria.Write(bytes.fromhex(self.RELAY_7_OFF))

    #def abnormalSharp(self):
    #    self.abnormalFlag=True
    #    on=False
    #    while self.abnormalFlag:
    #        time.sleep(0.5)
    #        if(on):
    #            on=False
    #            self.seria.Write(bytes.fromhex(self.RELAY_3_OFF))
    #        else:
    #            on=True
    #            self.seria.Write(bytes.fromhex(self.RELAY_3_ON))
                    








